package com.ovt.doorbell.payment.dao;

import java.sql.Timestamp;
import java.text.MessageFormat;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ovt.doorbell.payment.common.utils.DataConvertUtils;
import com.ovt.doorbell.payment.dao.constant.MemberResourceStatus;
import com.ovt.doorbell.payment.dao.constant.ResourceType;
import com.ovt.doorbell.payment.dao.mapper.MemberResourceMapper;
import com.ovt.doorbell.payment.dao.vo.MemberResource;
import com.ovt.doorbell.payment.dao.vo.PageInfo;

@Repository
public class MemberResourceDaoImpl implements MemberResourceDao
{
    @Autowired
    private DaoHelper daoHelper;
    @Autowired
    private MemberResourceMapper memberResourceMapper;

    private String SQL_GET_PREFIX = "SELECT id,user_id,res_type,"
            + "res_size,mr_start_time,mr_expire_time,mr_end_time,"
            + "mr_status,order_no,create_time,update_time "
            + "FROM member_resource ";

    private String SQL_GET_MEMBER_RESOURCE_LIST = SQL_GET_PREFIX
            + "order by {0} {1} LIMIT {2}, {3}";

    private String SQL_GET_MEMBER_RESOURCE_BY_USERID = SQL_GET_PREFIX
            + "WHERE user_id = ?";

    private String SQL_INSERT_MEMBER_RESOURCE = "INSERT INTO "
            + "member_resource(user_id,res_type,res_size,mr_start_time,mr_expire_time,mr_end_time,"
            + "mr_status,order_no,create_time,update_time) "
            + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)";

    private String SQL_DELETE_MEMBER_RESOURCE = "DELETE FROM member_resource WHERE id = ?";

    private String SQL_UPDATE_STATUS = "UPDATE member_resource SET mr_status = ? WHERE id = ?";

    private String SQL_UPDATE_END_TIME = "UPDATE member_resource SET mr_endtime = ? WHERE id = ?";

    @Override
    public void save(MemberResource memberResource)
    {
        Object[] params = new Object[8];
        params[0] = memberResource.getUserId();
        params[1] = memberResource.getResourceType();
        params[2] = memberResource.getResourceSize();
        params[3] = memberResource.getStartTime();
        params[4] = memberResource.getExpireTime();
        params[5] = memberResource.getEndTime();
        params[6] = MemberResourceStatus.getTypeString(memberResource.getStatus());
        params[7] = memberResource.getOrderNo();

        String errMsg = MessageFormat.format(
                "Failed insert member resource, userId={0}!",
                memberResource.getUserId());

        this.daoHelper.save(SQL_INSERT_MEMBER_RESOURCE, errMsg, true, params);
    }

    @Override
    public List<MemberResource> getMemberResourceList(PageInfo page)
    {
        String sql = MessageFormat.format(SQL_GET_MEMBER_RESOURCE_LIST,
                page.getSortBy(), page.getOrder().toString(),
                DataConvertUtils
                        .toString(page.getPageNo() * page.getPageSize()),
                DataConvertUtils.toString(page.getPageSize()));

        String errMsg = MessageFormat.format(
                "Failed to query member resource list with page info: {0}!",
                page);

        List<MemberResource> memberResources = daoHelper.queryForList(sql,
                memberResourceMapper, errMsg);

        return memberResources;
    }

    @Override
    public List<MemberResource> getMemberResourceByUserId(PageInfo page,
            String userId)
    {
        String sql = MessageFormat.format(SQL_GET_MEMBER_RESOURCE_BY_USERID,
                page.getSortBy(), page.getOrder().toString(),
                DataConvertUtils
                        .toString(page.getPageNo() * page.getPageSize()),
                DataConvertUtils.toString(page.getPageSize()));

        String errMsg = MessageFormat.format(
                "Failed to query member resource list with page info: {0}!",
                page);

        List<MemberResource> memberResources = daoHelper.queryForList(sql,
                memberResourceMapper, errMsg);

        return memberResources;
    }

    @Override
    public void updateEndTime(Timestamp endTime, int id)
    {
        String errMsg = MessageFormat.format(
                "Failed to update member resource id:{0} with end time: {1}!",
                id, endTime);

        this.daoHelper.update(SQL_UPDATE_END_TIME, errMsg, endTime, id);
    }

    @Override
    public void delete(int id)
    {
        String errMsg = MessageFormat
                .format("Failed to delete member resource id:{0}!", id);

        this.daoHelper.update(SQL_DELETE_MEMBER_RESOURCE, errMsg, id);
    }

    @Override
    public void updateStatus(int id, MemberResourceStatus status)
    {
        String errMsg = MessageFormat.format(
                "Failed to update member resource id:{0} with status: {1}!", id,
                status);
        String statusStr = MemberResourceStatus.getTypeString(status);
        this.daoHelper.update(SQL_UPDATE_STATUS, errMsg, statusStr, id);
    }

}
